package org.gjy.m8.stream;

import com.google.common.collect.Lists;
import lombok.Builder;
import lombok.Data;
import org.junit.Test;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-08-02 09:48:50
 */
public class OptionTest {

    @Test
    public void test5() {
        AtomicInteger integer = new AtomicInteger(0);
        do {
            integer.incrementAndGet();
            System.out.println(integer);
        } while (integer.get() < 3);
        System.out.println(integer);
    }

    @Test
    public void test4() {
        String format = DateTimeFormatter.ofPattern("yyMMdd").format(LocalDate.now());
        System.out.println(format);
    }

    @Test
    public void test3() {
        String s = generateOrderNo();
        System.out.println(s);
    }

    public static String generateOrderNo() {
        int hashCode = UUID.randomUUID().toString().hashCode();
        //可能为负数
        if (hashCode < 0) {
            hashCode = -hashCode;
        }
        return String.format("%04d", hashCode).substring(0, 4);
    }

    @Test
    public void test2() {
        ArrayList<A> list = Lists.newArrayList(
                A.builder().id(1).name("aa").build(),
                A.builder().id(2).name("bb").build(),
                A.builder().id(3).name("cc").build(),
                A.builder().id(4).name("dd").build()
        );
        Map<Integer, A> collect = list.stream().collect(Collectors.toMap(A::getId, (a) -> a));
        System.out.println(collect);
    }

    @Data
    @Builder
    private static class A {
        private Integer id;
        private String name;
    }

    @Test
    public void test1() {
        Optional.of("Hello World")
                .map(s -> s.split(" "))
                .ifPresent(array -> {
                    System.out.println(array.length);
                    System.out.println(Arrays.toString(array));
                });
    }
}
